
//题目：找出字符串中，连续重复次数最多的字符
//let str = "acaaccabbcccbbbcccccccccccccdddccdddccccc"

/* 
let reg = /c+/g

let res = []

while ((cur = reg.exec(str)) !== null) {
    res.push(cur[0])
}

res.sort(function (a, b) {
    return a.length - b.length
}) 

console.log(res[res.length - 1], res[res.length - 1].length)
*/

function findLongest(str, chacharactersrs) {
  let all = []

  //遍历
  characters.forEach(function (character) {

    //创建一个正则表达式
    let regStr = `${character}+`
    let reg = new RegExp(regStr, 'g')

    let res = []

    //找出所有匹配项
    while ((match = reg.exec(str)) !== null) {
      res.push(match[0])
    }

    //排序
    res.sort(function (a, b) {
      return a.length - b.length
    })

    //取出最大的重复次数值
    let obj = {}
    obj.char = character
    obj.largetLength = res[res.length - 1].length

    //保存起来
    all.push(obj)

  })

  //排序
  all.sort(function (a, b) {
    return a.largetLength - b.largetLength
  })

  //取出重复次数 最多的那个字符
  let largetLengthChar = all[all.length - 1].char

  return largetLengthChar
}

let str = "acaaccabbcccbbbcccccccccccccdddccdddccccc"

//在字符串中出现的字符 
let characters = ['a', 'b', 'c', 'd']
let result = findLongest(str, characters)
console.log('字符串 ' + str + ' 中连续重复次数最多的字符是：' + result)
